package com.ap.core.domain;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

import javax.persistence.Embeddable;
import java.io.Serializable;
import java.math.BigDecimal;

/**
 * This represents a monetary rate with zero precision loss.
 */
@Embeddable
public final class Rate implements Serializable {

    /** Generated serial version UID */
     static final int DECIMAL_PRECISION = 2;

    /** {@link java.math.BigDecimal} value for the {@link Rate} */
    private BigDecimal value;


    private Rate() {
        // Do nothing constructor to keep hibernate happy
    }

    public Rate(final BigDecimal newValue) {
        this.value = newValue;
    }

    public static Rate valueOf(final BigDecimal value) {
        return new Rate(value);
    }

    /**
     * Instantiates a new amratesing the value of the Amount passed to the constructor
     *
     */
    public Rate(final Rate rate)  {
        if (rate != null) {
            this.value = rate.getValue();
        }
    }

    /**
     * Gets the {@link java.math.BigDecimal} value for the amount
     *
     * @return the {@link java.math.BigDecimal} value
     */
    public BigDecimal getValue() {
        return value;
    }


    public int hashCode() {
        return new HashCodeBuilder().append(this.value).toHashCode();
    }


    public boolean equals(final Object obj) {
        boolean retVal = false;
        if (obj != null) {
            if (obj == this) {
                retVal = true;
            } else {
                retVal = obj.getClass() == getClass() && new EqualsBuilder().append(this.value, ((Rate) obj).value).isEquals();
            }
        }
        return retVal;
    }


    public String toString() {
        return value.toString();
    }

}
